/**
 *
 * 日期界面
 */

;define([
    '../DateBox/extend.js',
    '../DateBox/Tit.js',
    '../DateBox/compareDate.js'
],function(
    extend,
    Tit,
    compareDate
){

    var DateComponent = function(dateBox,opts,objs,sizeOptis){
        var box = this.box = document.createElement('div');
        dateBox.container.appendChild(box);
        this.dateBox = dateBox;

        var opts = this.opts = extend(opts||{},{
            width:sizeOptis.width,				//选择日期窗口宽度
            height:sizeOptis.height,				//选择日期窗口高度

            dateButoonFontSize:8,	//选择日期按钮字体大小
            dateButoonWidth:20,		//选择日期按钮宽度
            dateButoonHeight:17,	//选择日期按钮高度
            weekTitBackground:'#f8f8f8',		//工作日标题背景颜色

            dateButtonBackground:'#fff',		//选择日期按钮背景颜色
            dateButtonHoverBack:'#e6e6e6',		//选择日期按钮鼠标移动到上面的背景颜色
            titStyle:{

            },

            //  MIN MAX
            minDatas: sizeOptis.minDatas,
            maxDatas: sizeOptis.maxDatas,
            objs:objs,
        });

        //初始化DOM元素
        this.initDOM();
        //初始化样式
        this.initStyle();
        //根据dateBox更新内容
        this.update(dateBox.dateTime);
        //添加事件
        this.addEL();
    };

    DateComponent.prototype.addTit = function(){
        var opts = this.opts.titStyle;
        var _this = this;

        var tit = this.tit = new Tit(opts);
        tit.addEL({
            left:function(){
                _this.dateBox.dateTime.plusMonth(-1);
            },
            right:function(){
                _this.dateBox.dateTime.plusMonth();
            }
        });
        this.box.appendChild(tit.obj);
    };
    DateComponent.prototype.update = function(date){
        //初始化区间时间
        // console.log(date);
        var _this = this;
        var min = this.opts.minDatas.split('-');
        var max = this.opts.maxDatas.split('-');
        var monthStr =['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月'];
        // date.month = min[1];
        this.tit.setText(date.year + '年' + ' ' +monthStr[date.month-1]);
        var dates = date.getMonthDates();
        for(var i = 0;i<42;i++){
            _this.dates[i].innerHTML = dates[i].date;
            var dm = dates[i].dm;
            if(date.year >= min[0] && date.year <= max[0]){
                if(date.month == max[1]){   // 等于最大月份
                    if(min[1] < date.month){
                        if(dates[i].date <= parseInt(max[2])){
                            if(dm == 0){
                                dm = 2;
                            }
                        }
                    }else{
                        if(dates[i].date >= parseInt(min[2]) && dates[i].date <= parseInt(max[2])){
                            if(dm == 0){
                                dm = 2;
                            }
                        }
                    }
                }else if(date.month > min[1] && date.month < max[1]){
                    if(dm == 0){
                        dm = 2;
                    }
                }else if(date.month == min[1]){
                    if(dates[i].date >= parseInt(min[2])){
                        if(dm == 0){
                            dm = 2;
                        }
                    }
                }
            }
            _this.dates[i].index = (dm ==2 ) ? 0:dm;
            if(dm == 1 || dm == -1){
                _this.dates[i].style.color = '#787878';
            }else if(dm == 0){
                _this.dates[i].style.color = '#000000';
            }else{
                _this.dates[i].style.color = '#377CFF';
            }
        }
    };

    DateComponent.prototype.initDOM = function(){
        this.box.innerHTML='';
        this.addTit();
        var con = this.con = document.createElement('div');
        this.box.appendChild(con);

        var weekStr = ['日','一','二','三','四','五','六'];
        var weeks = this.weeks = [];
        var dates = this.dates = [];

        for(var i=0;i<7;i++){
            var div = document.createElement('div');
            div.innerHTML = weekStr[i];
            con.appendChild(div);
            weeks.push(div);
        };
        for(var i=0;i<42;i++){
            var div = document.createElement('div');
            con.appendChild(div);
            dates.push(div);
        };

    };

    DateComponent.prototype.show = function(){
        this.box.style.display = 'block';
    };
    //修改元素是否显示
    DateComponent.prototype.hide = function(){
        this.box.style.display = 'none';
    };

    DateComponent.prototype.initStyle = function(){
        var _this = this;
        var bs = this.box.style;
        bs.float = 'left';
        bs.width = parseInt(this.opts.width) + 'px';
        bs.height = parseInt(this.opts.height) + 'px';

        var cs = this.con.style;
        cs.overflow = 'hidden';

        for(var i=0;i<7;i++){
            var divs = this.weeks[i].style;
            commonStyle(divs);
            divs.background = _this.opts.weekTitBackground;
        };
        for(var i=0;i<42;i++){
            var divs = this.dates[i].style;
            divs.cursor = 'pointer';
            commonStyle(divs);
            divs.background = _this.opts.dateButtonBackground;
        };

        function commonStyle(objs){
            objs.float = 'left';
            objs.fontSize = parseInt(_this.opts.dateButoonFontSize) + 'px';
            objs.width = parseInt(_this.opts.dateButoonWidth) + 'px';
            objs.height = objs.lineHeight = parseInt(_this.opts.dateButoonHeight) + 'px';
            objs.textAlign = 'center';
        }
    };
    DateComponent.prototype.addEL = function(){
        var _this = this;
        var minDatas = this.opts.minDatas;
        var maxDatas = this.opts.maxDatas;
        var maxDate = _this.dateBox.opts.maxDate == 'today'?_this.dateBox.dateTime.getTodayStr(): _this.dateBox.opts.maxDate;
        for(var i=0;i<42;i++){
            (function(index){
                _this.dates[index].addEventListener('click',function(){
                    var date = {
                        year:_this.dateBox.dateTime.year,
                        month:_this.dateBox.dateTime.month + this.index,
                        date: Number(this.innerHTML)
                    };
                    //检测日期是否在范围之内
                    if(!compareDate(date,minDatas))return;
                    if(!compareDate(maxDatas
                        ,date))return;
                    _this.dateBox.dateTime.setDateTime(date);
                    //定义新属性  更改value
                    _this.opts.objs.setAttribute("value",date.year+'-'+date.month+'-'+date.date);

                    _this.dateBox.setFooter(_this.dateBox.dateTime.toString('y-d'));
                    if(_this.dateBox.hourComponent){
                        _this.dateBox.hourComponent.show();
                    }else{
                        _this.dateBox.initCS();
                        _this.dateBox.inputTime();
                    }
                });
                _this.dates[index].addEventListener('mouseover',function(){
                    this.style.cursor = 'pointer';
                    var date = {
                        year:_this.dateBox.dateTime.year,
                        month:_this.dateBox.dateTime.month + this.index,
                        date: Number(this.innerHTML)
                    };
                    if((!compareDate(date,minDatas))||(!compareDate(maxDatas,date))){
                        this.style.cursor = 'not-allowed';
                    }else{
                        this.style.cursor = 'pointer';
                        this.style.background = _this.opts.dateButtonHoverBack;
                    };
                });
                _this.dates[index].addEventListener('mouseout',function(){
                    this.style.background = _this.opts.dateButtonBackground;
                });
            })(i);
        };

    };
    return DateComponent;
});
